package com.dididoctor.doctor.Activity.Main.Inquiry.InquiryDetail;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.dididoctor.doctor.Activity.Main.Inquiry.NewInquiry.NewInquiryActivity;
import com.dididoctor.doctor.MV.EduActivity;
import com.dididoctor.doctor.R;
import com.dididoctor.doctor.Utils.Util;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/4/19.
 */
public class InquiryDetailActivity extends EduActivity implements InquiryDetailView, InquiryDetailAdapter.DeleteClick {

    private String titlename = "", diseId = "";
    private List<InquiryDetail> details = new ArrayList<>();
    private ListView listView;
    private InquiryDetailAdapter adapter;
    private InquiryDetailPresenter presenter;
    private static final int TO_NEWQUESTION = 1002;


    @Override
    public void init() {
        titlename = getIntent().getStringExtra("titlename");
        diseId = getIntent().getStringExtra("diseId");
        setText(R.id.tv_title, titlename);

        listView = (ListView) findViewById(R.id.listview);
        adapter = new InquiryDetailAdapter(this, details, diseId, this);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if ("0".equals(details.get(position).getIsSys())) {
                    Intent intent = new Intent(InquiryDetailActivity.this, NewInquiryActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("inquiry", details.get(position));
                    intent.putExtra("diseId", diseId);
                    intent.putExtras(bundle);
                    startActivityForResult(intent, TO_NEWQUESTION);
                }
            }
        });
        presenter = new InquiryDetailPresenter(this, this);
        presenter.getInqDet(diseId);

        findViewById(R.id.btn_confirm).setOnClickListener(this);

    }

    @Override
    public void onClickEvent(View view) {
        switch (view.getId()) {
            case R.id.btn_confirm:
                Intent intent = new Intent(this, NewInquiryActivity.class);
                intent.putExtra("diseId", diseId);
                intent.putExtra("optype", "1");
                startActivityForResult(intent, TO_NEWQUESTION);
                break;
        }
    }

    @Override
    public void setContentLayout() {
        setContentView(R.layout.activity_inquiry_detail);
    }

    @Override
    public void getinqDet(List<InquiryDetail> details) {
        //拆分
        List<InquiryDetail> detailsone = new ArrayList<>();
        List<InquiryDetail> detailstwo = new ArrayList<>();
        if (!Util.isEmpty(details)) {
            for (int i = 0; i < details.size(); i++) {
                InquiryDetail inquiryDetail = details.get(i);
                if ("1".equals(inquiryDetail.getIsSys())) {//系统
                    detailsone.add(inquiryDetail);
                } else {
                    detailstwo.add(inquiryDetail);
                }
            }

            for (int i = 0; i < detailsone.size(); i++) {
                if (i == detailsone.size() - 1) {
                    InquiryDetail inquiryDetail = detailsone.get(i);
                    inquiryDetail.setIsshow(true);
                }
            }
            this.details.clear();
            this.details.addAll(detailsone);
            this.details.addAll(detailstwo);
        }
        adapter.setData(this.details);
        adapter.notifyDataSetChanged();
    }


    @Override
    public void getinqDetFail() {
        showToastMessage("请求失败");
    }

    @Override
    public void DeleQuestionSucced() {
        presenter.getInqDet(diseId);
    }

    @Override
    public void DeleQuestionFail() {
        showToastMessage("删除失败");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == TO_NEWQUESTION && resultCode == RESULT_OK) {
            presenter.getInqDet(diseId);
        }
    }

    @Override
    public void deleteinquiry(final String qusId) {
        IsDeleteDialog isDeleteDialog = new IsDeleteDialog(this, new IsDeleteDialog.IsDeteltlist() {
            @Override
            public void isdelete() {
                presenter.qusoperate(qusId);
            }
        });
        isDeleteDialog.show();

    }
}
